package com.jn.mvvm2.repository;

import com.jn.mvvm2.entity.News;
import com.jn.mvvm2.service.NewsService;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class NewsRepository {
    public static final String NEWS_BASE_URL = "https://apis.juhe.cn/";
    public static final String KEY = "ee2f47b4bd7990ff8e68d7279f93b7e7";
    private NewsService mNewsService;

    public NewsRepository() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(NEWS_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();
        mNewsService = retrofit.create(NewsService.class);
    }

    public Observable<News> getData() {
        return mNewsService.getData(KEY)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io());
    }
}
